package com.wjf.accesscontrol

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.common.apiutil.util.SDKUtil
import com.wjf.accesscontrol.util.Constant
import com.wjf.accesscontrol.util.SPUtils
import com.wjf.accesscontrol.util.SP_KEY_BASE_URL
import com.wjf.accesscontrol.util.SP_KEY_MACHINE_ID
import com.wjf.accesscontrol.util.SP_KEY_MANAGER_URL
import com.wjf.accesscontrol.util.ScreenAdaptUtils
import java.util.concurrent.atomic.AtomicInteger

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2023/12/15 11:08
 */
class MyApplication : Application() {


    companion object {
        lateinit var instance: MyApplication
    }

    var aliveActivity = AtomicInteger(0)

    override fun onCreate() {
        super.onCreate()
        ScreenAdaptUtils(this, 800f, 0).register()
        instance = this

        Constant.machineId = SPUtils.instance.getString(SP_KEY_MACHINE_ID)
        if (SPUtils.instance.getString(SP_KEY_BASE_URL).isNotEmpty()){
            Constant.baseUrl = SPUtils.instance.getString(SP_KEY_BASE_URL)
        }
        Constant.managerUrl = SPUtils.instance.getString(SP_KEY_MANAGER_URL,Constant.managerUrl)

        SDKUtil.getInstance(this).initSDK()


        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}

            override fun onActivityStarted(activity: Activity) {}

            override fun onActivityResumed(activity: Activity) {
                aliveActivity.addAndGet(1)
            }

            override fun onActivityPaused(activity: Activity) {}

            override fun onActivityStopped(activity: Activity) {
                aliveActivity.decrementAndGet()
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}

            override fun onActivityDestroyed(activity: Activity) {}
        })
    }
}